package com.gx.security.Symmetric_encryption;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.KeySpec;

/**
 * Created by gx on 2017/3/29.
 */
public class PBETest {
    private static String src = "imooc security PBE";
    private static String password = "gaoxing";

    public static void main(String[] args) {
        jdkPBEtest();
        bcPBEtest();
    }

    public static void jdkPBEtest() {
        try {
            //初始化盐
            SecureRandom random = new SecureRandom();
//           设置8位 salt
            byte[] salt = random.generateSeed(8);

//        口令和秘钥
            KeySpec PBEkeyspec = new PBEKeySpec(password.toCharArray());

//        实例化key工厂
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
            //密码转化秘钥
            Key key = factory.generateSecret(PBEkeyspec);
//                                                salt值   加密次数
            PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);
            //            加密
//            设置加密模式
            Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
//            初始化加密模式 和内容
            cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("JDK PBE encrypt:" + Base64.encodeBase64String(result));
//            重新解密
            cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec);
            result = cipher.doFinal(result);
            System.out.println("JDK PBE decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void bcPBEtest() {
        try {
            Security.addProvider(new BouncyCastleProvider());
            //初始化盐
            SecureRandom random = new SecureRandom();
//           设置8位 salt
            byte[] salt = random.generateSeed(8);

//        口令和秘钥
            KeySpec PBEkeyspec = new PBEKeySpec(password.toCharArray());

//        实例化key工厂                                         指定Provider(提供者)
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES","BC");
            //密码转化秘钥
            Key key = factory.generateSecret(PBEkeyspec);
//                                                salt值   加密次数
            PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);
            //            加密
//            设置加密模式
            Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
//            初始化加密模式 和内容
            cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec);
            byte[] result = cipher.doFinal(src.getBytes());
            System.out.println("BC PBE encrypt:" + Base64.encodeBase64String(result));
//            重新解密
            cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec);
            result = cipher.doFinal(result);
            System.out.println("BC PBE decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
